package com.pearl.jacksoncore.demo.feature;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.json.JsonReadFeature;
import com.fasterxml.jackson.databind.json.JsonMapper;

import java.io.IOException;

/**
 * @author TD
 * @version 1.0
 * @date 2024/3/29
 */
public class JsonFactoryFeatureTest {
    public static void main(String[] args) throws IOException {
        // 构建 JsonFactory
        //
        //JsonFactory jsonFactory = JsonFactory.builder()
        //        .configure(JsonFactory.Feature.INTERN_FIELD_NAMES, true)// 配置特性
        //        .enable(JsonFactory.Feature.CANONICALIZE_FIELD_NAMES)// 开启特性
        //        .disable(JsonFactory.Feature.CHARSET_DETECTION)// 关闭特性
        //        .build();
        //
        // ``
        //JsonFactory jsonFactory = new JsonFactory();
        //jsonFactory.configure(JsonFactory.Feature.INTERN_FIELD_NAMES,true); // 配置特性（已过时）推荐使用 JsonFactory.builder()
        //jsonFactory.disable(JsonFactory.Feature.INTERN_FIELD_NAMES); // 关闭特性（已过时）
        //jsonFactory.enable(JsonFactory.Feature.INTERN_FIELD_NAMES); // 开启特性（已过时）
        //boolean enabled = jsonFactory.isEnabled(JsonFactory.Feature.INTERN_FIELD_NAMES); // 查询某个特性是否开启

        // 构建 JsonFactory
        User user = new User();
        user.setId(1767798780627279333L);
        user.setName("https://www.baidu.com/");
        user.setaDouble(Double.NaN);
        JsonFactory jsonFactory = JsonFactory.builder()
                .configure(JsonReadFeature.ALLOW_MISSING_VALUES, true)// 配置特性
                .build();
        // JsonWriteFeature
        JsonMapper jsonMapper = new JsonMapper(jsonFactory);
        String jsonStr = jsonMapper.writeValueAsString(user);
        System.out.println(jsonStr);

        // JsonReadFeature
/*        String jsonWithLeadingPlusSign = "{\"id\": ,\"name\": \"老八\",}";
        User userByComments = jsonMapper.readValue(jsonWithLeadingPlusSign, User.class);
        System.out.println(userByComments);*/



    }
}





